package com.zhongsou.souyue.filemanager;

import android.os.Environment;
import android.util.Log;

import java.io.File;

/**
 * Created by zcz on 2015/5/27.
 */
public class FileUtils {
    public static String debugDir = getCacheDir()+File.separator+"souyue"+File.separator+"debugLog"+File.separator;
    public static String crashDir = getCacheDir()+File.separator+"souyue"+File.separator+"crashLog"+File.separator;

    /**
     * 得到SDCard的根目录
     *
     * @return
     */
    private static File getCacheDir() {
        if (isExistSDCard()) {
            return Environment.getExternalStorageDirectory();
        } else if(Environment.getDataDirectory().exists() && Environment.getDataDirectory().canWrite() && Environment.getDataDirectory().canRead()){
            return Environment.getDataDirectory();
        }else{
            throw new IllegalStateException("存储位置不可用");
        }
    }

    /**
     * 判断文件是否已经存在了,存在返回true,不存在返回false;
     *
     * @param
     * @return true if this file is exist
     */

    public static boolean ifExistFile(String fileName) {
        return new File(fileName).exists();
    }

    /**
     * 判断SDCard是否被挂载,是否能正常使用
     *
     * @return true if SDCard we can used
     */

    public static boolean isExistSDCard() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 得到日志目录下所有文件以便上传
     * @return
     */
    public static String [] getFileNameList(SaveType saveType){
        File file=null;
        if(saveType == SaveType.SAVE_DEBUG_FILE_TYPE){
            file = new File(debugDir);
        }else if(saveType == SaveType.SAVE_CRASH_FILE_TYPE){
            file = new File(crashDir);
        }else{
            Log.e("FileUtils","get fileNameList error .  can't fond this saveType ="+saveType);
            return null;
        }
        return file.list();
    }


}
